package com.im.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.im.service.dto.BaseException;
import com.im.service.dto.MeteDeclare;
import com.im.service.dto.PageQuery;
import com.im.service.meteDeclare.smo.IMeteDeclareOperSMO;
import com.im.service.meteDeclare.smo.IMeteDeclareQuerySMO;
import com.im.util.ReturnsUtil;
import com.im.util.StringUtil;

import net.sf.json.JSONObject;

/**
 * 计量申请后端服务.

 * @author wangxh
 * 2018年3月21日
 */
@RestController
@RequestMapping("/meteDeclareService")
public class MeteDeclareService {

    private static final Logger logger = LogManager.getLogger(ProjectInfoService.class);

    @Autowired
    private IMeteDeclareQuerySMO meteDeclareQuerySMO;

    @Autowired
    private IMeteDeclareOperSMO meteDeclareOperSMO;

    /**
	 *
	 * 计量申报审核列表查询.
	 *
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
    @PostMapping(value = "/queryMeteDeclareList")
    public String queryMeteDeclareList(@RequestBody String jsonStr){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参json:{}", jsonStr);
        }

        try {
            if (jsonStr == null || jsonStr.length() <= 0) {
                throw new BaseException("请求入参为空，请核查数据!");
            }

            PageQuery<MeteDeclare> result = meteDeclareQuerySMO.queryMeteDeclareList(jsonStr);
            return ReturnsUtil.returnSuccess(result).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

    /**
     *
     * 查询采购计划详情.
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/queryMeteDeclareDetail")
    public String queryMeteDeclareDetail(@RequestBody MeteDeclare meteDeclare){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", meteDeclare.toString());
        }
        try {
            if (meteDeclare == null || StringUtil.isEmpty(meteDeclare.getDeclareId())) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            MeteDeclare result = meteDeclareQuerySMO.queryMeteDeclareDetail(meteDeclare);
            return ReturnsUtil.returnSuccess(result).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }


    /**
     *
     *  新增采购计划信息
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/addMeteDeclare")
    public String addMeteDeclare(@RequestBody MeteDeclare meteDeclare){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", meteDeclare.toString());
        }
        try {
            if (meteDeclare == null) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            meteDeclareOperSMO.addMeteDeclare(meteDeclare);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

    /**
     *
     *  修改采购计划信息
     * @param meteDeclare
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/updateMeteDeclare")
    public String updateMeteDeclare(@RequestBody MeteDeclare meteDeclare){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", meteDeclare.toString());
        }
        try {
            if (meteDeclare == null || StringUtil.isEmpty(meteDeclare.getDeclareId())) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            meteDeclareOperSMO.updateMeteDeclare(meteDeclare);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

    /**
     *
     *  删除采购计划信息
     * @param meteDeclare
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/deleteMeteDeclare")
    public String deleteMeteDeclare(@RequestBody MeteDeclare meteDeclare){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参:{}", meteDeclare.toString());
        }
        try {
            if (meteDeclare == null || StringUtil.isEmpty(meteDeclare.getDeclareId())) {
                throw new BaseException("请求入参为空，请核查数据!");
            }
            meteDeclareOperSMO.deleteMeteDeclare(meteDeclare);
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage()).toString();
        }
    }

}
